package com.wisewin.pose.util.wechatOffice;

import com.wisewin.pose.util.wechatOffice.entity.WXTicket;
import com.wisewin.pose.util.wechatOffice.entity.WXToken;
import com.wisewin.pose.util.wechatOffice.exception.ApiException;
import com.wisewin.pose.util.wechatOffice.http.HttpUtils;
import com.wisewin.pose.util.wechatOffice.json.JSON;
import java.util.HashMap;
import java.util.LinkedHashMap;

/**
 * @author 溪浣君
 * 2024/10/31
 */
public class WechatApi {
    /**
     * 获取access_token
     */
    private static final String GET_ACCESS_TOKEN = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
    private static final String GET_USER_TOKEN = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";

    /**
     * 获取创建二维码ticket
     */
    private static final String GET_TICKET = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=%s";

    /**
     * 创建二维码
     */
    private static final String GET_QR_CODE = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=%s";

    /**
     * 获取用户信息
     */
    private static final String GET_USER_INFO = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s";

    public  String getAccessTokenUrl(String appId, String appSecret) {
        return String.format(GET_ACCESS_TOKEN, appId, appSecret);
    }
    public  String getUserTokenUrl(String code) {
        return String.format(GET_USER_TOKEN, appId, appSecret,code);
    }

    public  String getTicketUrl(String accessToken) {
        return String.format(GET_TICKET, accessToken);
    }

    public  String getQRCodeUrl(String ticket) {
        return String.format(GET_QR_CODE, ticket);
    }

    public  String getUserInfoUrl(String accessToken, String openId) {
        return String.format(GET_USER_INFO, accessToken, openId);
    }

    private  String appId;
    private  String appSecret;

    public WechatApi(String appId, String appSecret) {
        this.appId = appId;
        this.appSecret = appSecret;
    }

    /**
     * 获取后台调用接口的access_token
     * @return
     */
    public WXToken getAccessToken() {
        String url = getAccessTokenUrl(appId, appSecret);
        String s = HttpUtils.sendGet(url);
        validateResult(s);
        WXToken wxToken = JSON.json2Object(s, WXToken.class);
        return wxToken;
    }

    private void validateResult(String jsonResult){
        HashMap hashMap = JSON.json2Map(jsonResult);
        if(hashMap.get("errcode")!=null && Integer.parseInt(hashMap.get("errcode").toString())>0){
            throw new ApiException(hashMap.get("errmsg").toString());
        }
    }

    /**
     * 获取票据，以便能下一步获取二维码
     * @param token
     * @param scene
     * @return
     */
    public WXTicket getTicket(String token, String scene) {
        String s = HttpUtils.sendPost(getTicketUrl(token),getTicketJsonParam(scene));
        validateResult(s);
        return JSON.json2Object(s,WXTicket.class);
    }

    /**
     * 生成获取票据的参数
     * @param scene
     * @return
     */
    private String getTicketJsonParam(String scene)  {
        LinkedHashMap<String,Object> param=new LinkedHashMap<>();
        param.put("expire_seconds",60);
        param.put("action_name","QR_STR_SCENE");
        LinkedHashMap<String,String> sceneMap=new LinkedHashMap<>();
        sceneMap.put("scene_str",scene);
        LinkedHashMap<String,Object> actionInfoMap=new LinkedHashMap<>();
        actionInfoMap.put("scene",sceneMap);
        param.put("action_info",actionInfoMap);
        return JSON.object2Json(param);
    }

    public WXToken getUserToken(String code)  {
        String s = HttpUtils.sendGet(getUserTokenUrl(code));
        validateResult(s);
        WXToken wxToken = JSON.json2Object(s, WXToken.class);
        return wxToken;
    }
}
